<script setup>
import { ref } from 'vue'

// 设备列表
const devices = ref([
  {
    id: 1,
    name: '客厅音箱',
    type: 'speaker',
    icon: '🔊',
    online: true,
    power: true,
    volume: 60,
    playing: true,
    group: '客厅'
  },
  {
    id: 2,
    name: '卧室音箱',
    type: 'speaker',
    icon: '🔊',
    online: true,
    power: false,
    volume: 40,
    playing: false,
    group: '卧室'
  },
  {
    id: 3,
    name: '智能灯带',
    type: 'light',
    icon: '💡',
    online: true,
    power: true,
    brightness: 80,
    color: '#FF6B6B',
    group: '客厅'
  },
  {
    id: 4,
    name: '空气净化器',
    type: 'purifier',
    icon: '🌬️',
    online: true,
    power: true,
    mode: 'auto',
    quality: 'good',
    group: '客厅'
  }
])

// 设备分组
const groups = ref([
  {
    name: '全部',
    count: 4
  },
  {
    name: '客厅',
    count: 3
  },
  {
    name: '卧室',
    count: 1
  }
])

// 当前选中分组
const currentGroup = ref('全部')

// 设备状态统计
const deviceStats = ref({
  total: 4,
  online: 4,
  offline: 0,
  powerOn: 3
})

// 切换设备电源
const togglePower = (device) => {
  device.power = !device.power
  deviceStats.value.powerOn += device.power ? 1 : -1
}

// 调整音量
const updateVolume = (device, value) => {
  device.volume = value
}

// 调整亮度
const updateBrightness = (device, value) => {
  device.brightness = value
}

// 切换分组
const switchGroup = (group) => {
  currentGroup.value = group
}

// 添加设备
const showAddDevice = ref(false)
const toggleAddDevice = () => {
  showAddDevice.value = !showAddDevice.value
}
</script>

<template>
  <div class="devices-page">
    <!-- 状态概览 -->
    <div class="stats-overview">
      <div class="stat-card">
        <div class="stat-value">{{ deviceStats.total }}</div>
        <div class="stat-label">总设备数</div>
      </div>
      <div class="stat-card">
        <div class="stat-value">{{ deviceStats.online }}</div>
        <div class="stat-label">在线设备</div>
      </div>
      <div class="stat-card">
        <div class="stat-value">{{ deviceStats.offline }}</div>
        <div class="stat-label">离线设备</div>
      </div>
      <div class="stat-card">
        <div class="stat-value">{{ deviceStats.powerOn }}</div>
        <div class="stat-label">已开启</div>
      </div>
    </div>

    <!-- 设备管理 -->
    <div class="devices-section">
      <div class="section-header">
        <div class="groups-nav">
          <button
            v-for="group in groups"
            :key="group.name"
            class="group-btn"
            :class="{ active: currentGroup === group.name }"
            @click="switchGroup(group.name)"
          >
            {{ group.name }}
            <span class="count">{{ group.count }}</span>
          </button>
        </div>
        <button 
          class="add-btn"
          @click="toggleAddDevice"
        >
          添加设备
        </button>
      </div>

      <div class="devices-grid">
        <div 
          v-for="device in devices"
          :key="device.id"
          class="device-card"
          :class="{ offline: !device.online }"
        >
          <div class="card-header">
            <span class="device-icon">{{ device.icon }}</span>
            <div class="device-info">
              <h3>{{ device.name }}</h3>
              <span class="group-tag">{{ device.group }}</span>
            </div>
            <button 
              class="power-btn"
              :class="{ on: device.power }"
              @click="togglePower(device)"
            >
              ⚡
            </button>
          </div>

          <div class="device-status">
            <div 
              class="status-dot"
              :class="{ online: device.online }"
            ></div>
            <span class="status-text">
              {{ device.online ? '在线' : '离线' }}
            </span>
          </div>

          <!-- 音箱控制 -->
          <div 
            v-if="device.type === 'speaker'"
            class="device-controls"
          >
            <div class="control-item">
              <div class="control-header">
                <span>音量</span>
                <span class="value">{{ device.volume }}%</span>
              </div>
              <input
                v-model="device.volume"
                type="range"
                min="0"
                max="100"
                class="slider"
                @input="updateVolume(device, $event.target.value)"
              >
            </div>
            <div class="playback-status">
              {{ device.playing ? '正在播放' : '已暂停' }}
            </div>
          </div>

          <!-- 灯光控制 -->
          <div 
            v-if="device.type === 'light'"
            class="device-controls"
          >
            <div class="control-item">
              <div class="control-header">
                <span>亮度</span>
                <span class="value">{{ device.brightness }}%</span>
              </div>
              <input
                v-model="device.brightness"
                type="range"
                min="0"
                max="100"
                class="slider"
                @input="updateBrightness(device, $event.target.value)"
              >
            </div>
            <div class="color-picker">
              <input
                v-model="device.color"
                type="color"
                class="color-input"
              >
            </div>
          </div>

          <!-- 空气净化器控制 -->
          <div 
            v-if="device.type === 'purifier'"
            class="device-controls"
          >
            <div class="mode-selector">
              <button 
                class="mode-btn"
                :class="{ active: device.mode === 'auto' }"
              >
                自动
              </button>
              <button 
                class="mode-btn"
                :class="{ active: device.mode === 'sleep' }"
              >
                睡眠
              </button>
              <button 
                class="mode-btn"
                :class="{ active: device.mode === 'max' }"
              >
                强力
              </button>
            </div>
            <div class="air-quality">
              空气质量: {{ device.quality }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.devices-page {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

/* ���态概览样式 */
.stats-overview {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1.5rem;
}

.stat-card {
  background-color: var(--bg-light);
  border-radius: 12px;
  padding: 1.5rem;
  text-align: center;
}

.stat-value {
  font-size: 2rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
  background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.stat-label {
  color: var(--text-gray);
  font-size: 0.9rem;
}

/* 设备管理样式 */
.devices-section {
  background-color: var(--bg-light);
  border-radius: 16px;
  padding: 2rem;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

.groups-nav {
  display: flex;
  gap: 1rem;
}

.group-btn {
  padding: 0.5rem 1rem;
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-gray);
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.group-btn.active {
  background-color: var(--primary-color);
  border-color: var(--primary-color);
  color: var(--text-light);
}

.count {
  background-color: rgba(255, 255, 255, 0.1);
  padding: 0.2rem 0.5rem;
  border-radius: 10px;
  font-size: 0.8rem;
}

.add-btn {
  padding: 0.8rem 1.5rem;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.add-btn:hover {
  background-color: var(--secondary-color);
}

.devices-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1.5rem;
}

.device-card {
  background-color: var(--bg-dark);
  border-radius: 12px;
  padding: 1.5rem;
}

.device-card.offline {
  opacity: 0.6;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
}

.device-icon {
  font-size: 2rem;
}

.device-info {
  flex: 1;
}

.device-info h3 {
  margin: 0;
  margin-bottom: 0.3rem;
}

.group-tag {
  padding: 0.2rem 0.5rem;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  font-size: 0.8rem;
  color: var(--text-gray);
}

.power-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-gray);
  cursor: pointer;
  transition: all 0.3s ease;
}

.power-btn.on {
  background-color: var(--success-color);
  border-color: var(--success-color);
  color: white;
}

.device-status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--text-gray);
}

.status-dot.online {
  background-color: var(--success-color);
}

.status-text {
  color: var(--text-gray);
  font-size: 0.9rem;
}

.device-controls {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.control-item {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.control-header {
  display: flex;
  justify-content: space-between;
  color: var(--text-gray);
  font-size: 0.9rem;
}

.slider {
  width: 100%;
  height: 4px;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  cursor: pointer;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  background-color: var(--primary-color);
  border-radius: 50%;
  cursor: pointer;
}

.playback-status,
.air-quality {
  color: var(--text-gray);
  font-size: 0.9rem;
  text-align: center;
}

.color-picker {
  display: flex;
  justify-content: center;
}

.color-input {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
}

.mode-selector {
  display: flex;
  gap: 0.5rem;
}

.mode-btn {
  flex: 1;
  padding: 0.5rem;
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-gray);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.mode-btn.active {
  background-color: var(--primary-color);
  border-color: var(--primary-color);
  color: white;
}

@media (max-width: 768px) {
  .stats-overview {
    grid-template-columns: repeat(2, 1fr);
  }

  .section-header {
    flex-direction: column;
    gap: 1rem;
  }

  .groups-nav {
    width: 100%;
    overflow-x: auto;
    padding-bottom: 0.5rem;
  }

  .add-btn {
    width: 100%;
  }

  .devices-grid {
    grid-template-columns: 1fr;
  }
}
</style> 